package com.scfire.checkData

import net.sf.json.JSONObject

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import com.mongodb.BasicDBObject
import com.mongodb.DBCollection
import com.mongodb.DBCursor
import com.mongodb.DBObject
import com.scfire.fund.FDonate
import com.scfire.project.Donate
import com.scfire.project.Payment
import com.scfire.project.Project
import com.scfire.project.SubProject
import com.scfire.util.SCFIREConstants

class CheckMoneyAndRestService {
	def grailsApplication
	def mongo
	Log LOG = LogFactory.getLog(CheckMoneyAndRestService.class)
	def checkProjectList(){
		def DTCreate = Project.createCriteria()
		def projects
		def filterClosure = {
			or{
				eq("verified",SCFIREConstants.PROJECT_APPROVE_EXECUTE)
				eq("verified",SCFIREConstants.PROJECT_APPROVE_EXPIRED)
				eq("verified",SCFIREConstants.PROJECT_APPROVE_OVER)
				eq("verified","doing")
				eq("verified","pass")
			}
			order("id","asc")
		}
		try{
			projects = DTCreate.list(filterClosure)
		for(Project p:projects){
			def db = mongo.getDB(grailsApplication.config.grails.mongo.databaseName)
			DBCollection projectCollection = db.getCollection("donate")
			DBCollection projectCollection3 = db.getCollection("project")
			BasicDBObject queryCondition= new BasicDBObject()
			queryCondition.put("project",p.id)
			DBCursor cursor = projectCollection.find(queryCondition);
			
			List<String,String > mapTemp = new LinkedList<String,String>();
			while (cursor.hasNext()) {
				
				mapTemp.add(toHashMap(cursor.next()))
			}
			DBCollection projectCollection2 = db.getCollection("FDonate")
			BasicDBObject queryCondition2= new BasicDBObject()
			queryCondition2.put("project",p.id)
			DBCursor cursor2 = projectCollection2.find(queryCondition2);
			
			List<String,String > mapTemp2 = new LinkedList<String,String>();
			while (cursor2.hasNext()) {
				mapTemp2.add(toHashMap(cursor2.next()))
			}
			
			BigDecimal donateMoney = new BigDecimal("0");
			BigDecimal fdonateMoney = new BigDecimal("0");
			int payMoney =0;
			def donates =Donate.findAllByProject(p.id)
			def subProjects =SubProject.findAllByProject(p.id)
			def fdonates =FDonate.findAllByProject(p.id)
			float f
			if(donates){
				p.donateNum= donates.size()
				for(Map d:mapTemp){
					BigDecimal tempMoney = new BigDecimal(d.get("donate_fee"));
					donateMoney=donateMoney.add(tempMoney)
				}
			}else{
			p.donateNum= 0
			}
			if(subProjects){
				p.sbuProjectSize= subProjects.size()
			}else{
			p.sbuProjectSize= 0
			}
			if(fdonates){
				p.fDonateSize= fdonates.size()
				for(Map d:mapTemp2){
					BigDecimal tempMoney = new BigDecimal(d.get("donate_fee"));
					fdonateMoney=fdonateMoney.add(tempMoney)
				}
			}else{
			p.fDonateSize= 0
			}
			
			 
			def paymemts = Payment.findAllByProject(p.id)
				for(Payment p1:paymemts){
					payMoney+=p1.payMoney
				}
				
			
			
			   def project = Project.findById(p.id)
				double x1 = donateMoney.doubleValue();
				double x2 = fdonateMoney.intValue()/100
				double x3 = x1+x2
				double x4
				float progress   
				if(x3>p.amount){
					x4=0
					progress = 100
				}else{
					x4 = p.amount-x3
					progress = x3/p.amount
				}
				
				
				BigDecimal   b  =   new BigDecimal(progress);
				project.progress=  b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
				int count = Donate.countByProject(p.id)
				int count2 = FDonate.countByProject(p.id)
				println "id1:"+project.id+",项目名:"+project.name+",捐款人次:"+project.donateNum+",捐款总额:"+project.realAmount+",还剩："+project.rest+",配捐"+p.fDonateSize
				println "id2:"+p.id+",项目名:"+p.name+",捐款人次:"+count+",捐款总额:"+x3+",配捐："+count2
				DBObject queryCondition3= new BasicDBObject()
				queryCondition3.put("_id",p.id)
				BasicDBObject updateParameter =new BasicDBObject().append("\$set",new BasicDBObject().append("donateNum", count)
					.append("realAmount", x3)
					.append("fDonateSize", count2)
					.append("rest",x4)
					.append("progress",project.progress)
					)
				projectCollection3.update(queryCondition3, updateParameter,true,true)
				
			
		}
		
		
		}catch(Exception e){
		LOG.error("Project Management info catch exception. "+e.getMessage())
	}
	}
	
	private static HashMap<String, String> toHashMap(Object object)
	{
	 HashMap<String, String> data = new HashMap<String, String>();
	 // 将json字符串转换成jsonObject
	 JSONObject jsonObject = JSONObject.fromObject(object);
	 Iterator it = jsonObject.keys();
	 // 遍历jsonObject数据，添加到Map对象
	 while (it.hasNext())
	 {
	  String key = String.valueOf(it.next());
	  String value = (String) jsonObject.get(key);
	  data.put(key, value);
	 }
	 return data;
	}

	
	
	
	
	

	
}
